package com.cpi.cornpp.version.services;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

import org.apache.commons.lang.StringUtils;

import com.cpi.cornpp.common.exception.CornPPException;
import com.cpi.cornpp.version.converter.MonthPeriodVersionConverter;
import com.cpi.cornpp.version.converter.VersionConverter;
import com.cpi.cornpp.version.dao.VersionDAO;
import com.cpi.cornpp.version.dto.VersionDTO;
import com.cpi.cornpp.version.exception.VersionException;
import com.cpi.cornpp.version.view.VersionView;

public class VersionService {

	public List list(int orderField, boolean ascendingOrder) throws VersionException{	
		VersionDAO versionDAO = null;
		VersionConverter vConverter = null;
		List versionDTOs = null;
		List versionViews = null;
						
		try {
			versionDAO = new VersionDAO();
			versionDTOs = new ArrayList(0);
			versionViews = new ArrayList(0);
			vConverter = new VersionConverter();
			
			versionDTOs = versionDAO.list(orderField, ascendingOrder);			
			versionViews = vConverter.convertDTOs(versionDTOs);
		} catch (VersionException versionException) {
			throw versionException;
		} catch (Exception e){
			VersionException versionException = null;
			e.printStackTrace(System.out);
			versionException = new VersionException(e , CornPPException.LAYER_SERVICE,CornPPException.ACTION_LISTS);
			throw versionException;
		}			
		
		return versionViews;
	}
	
	public VersionView getVersion(Integer versionId) throws VersionException{
		VersionDTO versionDTO = null;
		VersionView versionView = null;
		VersionDAO versionDAO = null;
		VersionConverter versionConverter = null;
		
		try{
			
			if (versionId != null){
				versionDAO = new VersionDAO();
				versionConverter = new VersionConverter();
				versionDTO = versionDAO.getVersionById(versionId);
				versionView = versionConverter.convert(versionDTO);
			}
			
		} catch(VersionException ve){
			throw ve;
		} catch (Exception e){
			VersionException versionException = null;
			e.printStackTrace(System.out);
			versionException = new VersionException(e , CornPPException.LAYER_SERVICE,CornPPException.ACTION_LISTS);
			throw versionException;
		}
		
		return versionView;
	}
	
	public int updateVersion(VersionView versionView) throws VersionException{
		int updatedVersions = 0;
		VersionDTO versionDTO = null;
		VersionDAO versionDAO = null;
		VersionConverter versionConverter = null;
		
		try{
			
			if ((versionView != null) && (versionView.getVersionId() > 0) && StringUtils.isNotEmpty(versionView.getShortName()) && StringUtils.isNotEmpty(versionView.getDescription())){
				versionDAO = new VersionDAO();
				versionConverter = new VersionConverter();
				
				versionDTO = versionConverter.convert(versionView);
				updatedVersions = versionDAO.updateVersionById(versionDTO);
			}
			
		} catch(VersionException vee){
			throw vee;
		} catch (Exception e){
			VersionException vee = null;
			e.printStackTrace(System.out);
			vee = new VersionException(e , CornPPException.LAYER_SERVICE,CornPPException.ACTION_LISTS);
			throw vee;
		}
		
		return updatedVersions;
	}
	
	public int addVersion(VersionView versionView) throws VersionException{
		int addedVersions = 0;
		VersionDTO versionDTO = null;
		VersionDAO versionDAO = null;
		VersionConverter versionConverter = null;
		Calendar endDate = null;
		
		try{
			
			if ((versionView != null) && 
				(StringUtils.isNotEmpty(versionView.getShortName()) && StringUtils.isNotEmpty(versionView.getDescription()) && 
				(versionView.getStartDate() != null) && (versionView.getEndDate() != null))){
				
				versionDAO = new VersionDAO();
				versionConverter = new VersionConverter();
				
				versionDTO = versionConverter.convert(versionView);
				
				if (versionDTO.getEndDate() != null){
					endDate = Calendar.getInstance();
					
					endDate.setTime(versionDTO.getEndDate().getTime());
					endDate.add(Calendar.MONTH, 1);
					endDate.add(Calendar.DAY_OF_MONTH, -1);
					
					System.out.println(endDate);
					
					versionDTO.setEndDate(endDate);
				}
				addedVersions = versionDAO.insertVersion(versionDTO);
			}
			
		} catch(VersionException ve){
			throw ve;
		} catch (Exception e){
			VersionException versionException = null;
			e.printStackTrace(System.out);
			versionException = new VersionException(e , CornPPException.LAYER_SERVICE,CornPPException.ACTION_LISTS);
			throw versionException;
		}
		
		return addedVersions;
	}
	/**
	 * Delete a version
	 * @param versionId int
	 * @return TRUE > Record Deleted correctly , FALSE > delete fails
	 * @throws VersionException
	 */
	public void delete(int versionId) throws VersionException{
		VersionDAO versionDAO = null;
		try{
			versionDAO = new VersionDAO();
			versionDAO.delete(versionId);
		}
		catch (VersionException versionException){
			throw versionException;
		}
		catch (Exception exception){
			VersionException versionException = null;
			exception.printStackTrace();
			versionException = new VersionException (exception, CornPPException.LAYER_SERVICE,CornPPException.ACTION_DELETE);
			throw versionException;
		}
	}
	/**
	 * @author Edgar Joao
	 * @param id
	 * @return
	 * @throws VersionException
	 */
	public List getVersionsPeriods(int versionId) throws VersionException{		
		VersionDAO versionDAO = null;
		MonthPeriodVersionConverter monthPeriodVersionConverter = null;
		List listOfVersionsPeriods = null;		
		try{
			versionDAO = new VersionDAO();
			monthPeriodVersionConverter = new MonthPeriodVersionConverter();			
			listOfVersionsPeriods = monthPeriodVersionConverter.convertDTOsToViews(versionDAO.getVersionsPeriods(versionId));		
		} catch (VersionException versionException) {
			throw versionException;
		} catch (Exception e){
			VersionException versionException = null;
			e.printStackTrace(System.out);
			versionException = new VersionException(e , CornPPException.LAYER_SERVICE,CornPPException.ACTION_LISTS);
			throw versionException;
		}	
		return listOfVersionsPeriods;
	}
	
	public List getVersionsPeriods(int versionId, int periodId) throws VersionException{
		VersionDAO versionDAO = null;
		MonthPeriodVersionConverter monthPeriodVersionConverter = null;
		List listOfVersionsPeriods = null;		
		try{
			versionDAO = new VersionDAO();
			monthPeriodVersionConverter = new MonthPeriodVersionConverter();			
			listOfVersionsPeriods = monthPeriodVersionConverter.convertDTOsToViews(versionDAO.getVersionsPeriods(versionId, periodId));		
		} catch (VersionException versionException) {
			throw versionException;
		} catch (Exception e){
			VersionException versionException = null;
			e.printStackTrace(System.out);
			versionException = new VersionException(e , CornPPException.LAYER_SERVICE,CornPPException.ACTION_LISTS);
			throw versionException;
		}	
		return listOfVersionsPeriods;
	}
}